package com.example.plat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.plat.enity.Activity;
import com.example.plat.enity.Comment;
import com.example.plat.enity.Place;
import com.example.plat.exception.ActivityException;
import com.example.plat.mapper.GetActivityMapper;
import com.example.plat.service.GetActivityService;
import com.example.plat.service.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GetActivityServiceImpl implements GetActivityService {

    @Autowired
    GetActivityMapper mapper;

    @Autowired
    private PlaceService placeService;

    @Override
    public Activity lookActivity(Long actId) throws ActivityException {
        try {
            mapper.updateView(actId);
        } catch (Exception e) {
            throw new ActivityException("查看活动失败！");
        }
        return mapper.selectById(actId);
    }

    @Override
    public Activity getCreateActivity(Long actId) {
        return mapper.selectById(actId);
    }

    @Override
    public List<String> getImage(Long actId) {
        return mapper.getImage(actId);
    }

    @Override
    public List<Comment> getComment(Long actId) {
        return mapper.getComment(actId);
    }

    @Override
    public boolean isJoin(Long userId, Long actId) {
        Integer join = mapper.isJoin(userId, actId);
        return join > 0 ? true : false;
    }

    @Override
    public Place getPlace(Long placeId) {
        return placeService.getPlace(placeId);
    }

    @Override
    public IPage<Activity> findActivityByConditions(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        Page<Activity> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<>();
        lqw.like(condition.containsKey("title"), Activity::getTitle, condition.get("title"));
        lqw.like(condition.containsKey("content"), Activity::getContent, condition.get("content"));
        lqw.eq(condition.containsKey("time"), Activity::getTime, condition.get("time"));
        lqw.like(condition.containsKey("place"), Activity::getPlace, condition.get("place"));
        lqw.eq(condition.containsKey("isPass"), Activity::getIsPass, 1);
        if (condition.containsKey("category")) {
            if ("其它".equals(condition.get("category"))) {
                lqw.ne(Activity::getCategory, "文体活动");
                lqw.ne(Activity::getCategory, "学术竞赛");
                lqw.ne(Activity::getCategory, "大咖讲座");
            } else {
                System.out.println("--------");
                lqw.eq(Activity::getCategory, condition.get("category"));
            }
        }
        lqw.eq(condition.containsKey("isSurplus"), Activity::getIsSurplus, "isSurplus");

        //按热度排序
        lqw.orderByDesc(Activity::getHot);
        mapper.selectPage(page, lqw);
        return page;
    }

    @Override
    public IPage<Activity> search(String condition,Integer pageNum,Integer pageSize) {
        Page<Activity> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<>();
        lqw.like(Activity::getTitle,condition)
                .or().like(Activity::getContent,condition);
        lqw.eq(Activity::getIsPass,1);
        mapper.selectPage(page,lqw);
        return page;
    }
}
